﻿$.MGJ.WX_bing = [];


(function () {
    var _this = $.MGJ.WX_bing;
    $.extend($.MGJ.WX_bing, {
        //验证
        Ajax_Verify: function ()
        {
        
            var appId = $("#AppId").val();
            var appSecret = $("#AppSecret").val();
            $.L.ajaxHandle({
                type: "POST",
                url: ACTION_URL("Verify", "ConfigManager"),
                data: { Appid: appId, AppSecret: appSecret },
                success: function (response) {
                    var bootstrapValidator = $("#WX_AppInfo").data('bootstrapValidator').validate();
                    var data = JSON.stringify($("#WX_AppInfo").serializeObject());
                    if (bootstrapValidator.isValid()&& response.length > 100) {
                            $.MGJ.WX_bing.Ajax_AppSave(data, "app");
                    }
                    else {
                        $.L.msgError("验证失败 请检查Appid 或AppSecret是否正确！");
                    }
                   
                   
                }
            });
        },
        //添加
        Ajax_AppSave: function (data, type) {
      
            $.L.ajaxHandle({
                type: "POST",
                url: ACTION_URL("AppSave", "ConfigManager"),
                data: { submitData: data,Type:type },
                success: function (response) {
                  
                    var appId = $("#AppId").val();
                    var appSecret = $("#AppSecret").val();
                    if (type == "app") {
                        _this.Ajax_BindLoad(1);
                        $.L.msgSuccess("操作成功！");
                    }
                    else {
                      
                        var bootstrapValidator = $("#Merchant").data('bootstrapValidator').validate();
                        $("#fileName").val($("span.info").text());
                        console.log($("#fileName").val())
                        if ($("#fileName").val() != "") {
                            if (bootstrapValidator.isValid()) {
                                    if (response > 0) {
                                        _this.Ajax_BindLoad(2);
                                        $.L.msgSuccess("操作成功！");
                                    }
                            }
                            else {
                                $.L.msgError("验证失败 请检查Appid 或AppSecret是否正确！");
                            }
                        }
                        else {
                            $.L.msgError("请上传证书！");
                            return
                        }
                    }
                }
            });
        },
        //默认加载  如果有数据 就改为编辑状态
        Ajax_BindLoad: function (type)
        {

            $.L.ajaxHandle({
                url: ACTION_URL("BindLoad", "ConfigManager"),
                success: function (response) {
                  
                    //$("a[href='#Merchant']").click(function () {
                    //    var bootstrapValidator = $("#WX_AppInfo").data('bootstrapValidator').validate();
                    //    if (!bootstrapValidator.isValid()) {
                    //        $("a[href='#Merchant']").removeAttr('href data-toggle');
                    //    }
                    //    else {
                    //        $("ul>li:nth-child(2)>a").attr({ "href": "#Merchant", "data-toggle": "tab" });
                    //    }
                    //})
                  
                    if (response != 0) {
                        
                        if (type==1) {
                            $("#AppId").val(response.AppId);
                            $("#AppSecret").val(response.AppSecret);
                            tabState1 = response.AppState || 0;
                            _this.ViaTab1(tabState1);
                        }
                        else {
                            tabState2 = response.InfoState || 0;
                            _this.ViaTab2(tabState2);
                            $("#CommercialName").val(response.CommercialName);
                            $("#mchid").val(response.mchid);
                            $("#CommercialKey").val(response.CommercialKey);
                            $("#CredentialAddress").val(response.CredentialAddress);
                            $("#CredentialPwd").val(response.CredentialPwd);
                            if (response.CredentialAddress != null) {
                                var CredentialAddress = response.CredentialAddress.substring(response.CredentialAddress.lastIndexOf('/') + 1);
                                $("#uploaderBtnRow .item").remove();
                                $("#uploaderBtnRow").prepend('<div class="item" style="display:inline-block">' +
                                    '<span class="info">' + CredentialAddress + '</span>' +
                                    '</div>');
                                $("#picker2").hide();
                            }
                            else {
                                return
                            }
                        }
                    }
                    else {
                        if (type == 1) {
                            $("#saveBtn").show();
                        }
                        else {
                            $("#sunBtn2").show();
                        }
                        $("#checkoutImg").attr("src", "/Content/webImgs/Config/no.svg").show();
                    }
                 
                    //if (response.AppState || 0 == 1)
                    //    $("#checkoutImg").attr("src", "/Content/webImgs/Config/ok.svg");
                    //else
                    //    $("#checkoutImg").attr("src", "/Content/webImgs/Config/no.svg");
           
                }
            });
        },

        ViaTab1: function (state)
        {
           // response.AppState || 0 == 1
            if (state == 1) {
                $("#WX_AppInfo input").attr("readonly", "readonly");
                $("#editBtn").show();
                $("#saveBtn").hide();
                $("#checkoutImg").attr("src", "/Content/webImgs/Config/ok.svg");
            }
            else {
                $("#WX_AppInfo input").removeAttr("readonly");
                $("#checkoutImg").attr("src", "/Content/webImgs/Config/no.svg");
                $("#saveBtn").show();
                $("#editBtn").hide();
            }
            $("#checkoutImg").show();
        },
        ViaTab2: function (state) {
            // response.AppState || 0 == 1
            if (state == 1) {
                $("#Merchant input").attr("readonly", "readonly");
               // $("#picker2").hide();
                $("#editBtn2").show();
                $("#sunBtn2").hide();
                $("#checkoutImg").attr("src", "/Content/webImgs/Config/ok.svg");
            }
            else {
                $("#Merchant input").removeAttr("readonly");
                $("#checkoutImg").attr("src", "/Content/webImgs/Config/no.svg");
                $("#editBtn2").hide();
                $("#sunBtn2").show();
                $("#picker2").show();
            }
            $("#checkoutImg").show();
        }   
    });
})(jQuery);